package com.zls.mapper;

import com.zls.pojo.Project;
import com.zls.pojo.Stage;
import com.zls.pojo.Subitem;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface SubitemMapper {
    @Results(id = "SubitemMap", value = {
            @Result(column = "suid", property = "suid"),
            @Result(column = "sname", property = "sname"),
            @Result(column = "starttime", property = "starttime"),
            @Result(column = "endtime", property = "endtime"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "pid", property = "project", javaType = Project.class, one =
                    //FetchType.LAZY 懒加载   EAGER:立即加载
                    //select 代表的关联查询  调用指定的mapper方法
            @One(select = "com.com.zls.mapper.ProjectMapper.findByid", fetchType = FetchType.EAGER))
    })
    @Select("Select * from subitem where status =1")
    List<Subitem> findAll();

    @Results(id = "SubitemsMap", value = {
            @Result(column = "suid", property = "suid"),
            @Result(column = "sname", property = "suname"),
            @Result(column = "starttime", property = "starttime"),
            @Result(column = "endtime", property = "endtime"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "pid", property = "project", javaType = Project.class, one =
                    //FetchType.LAZY 懒加载   EAGER:立即加载
                    //select 代表的关联查询  调用指定的mapper方法
            @One(select = "com.com.zls.mapper.ProjectMapper.findByid", fetchType = FetchType.EAGER))
    })
    @Select("Select * from subitem where status =1 and pid=#{pid}")
    List<Subitem> findByPid(int pid);

    @ResultMap("SubitemMap")
    @Select("<script>" +
            "select * from subitem where status =1" +
            "<if test='queryString!=null'> and sname like concat( '%',#{queryString},'%')" +
            "limit #{currentPage},#{pageSize}</if>" +
            "<if test='queryString==null'>limit #{currentPage},#{pageSize}</if> "+
            "</script>")
    List<Subitem> findSub(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize, @Param("queryString") String queryString);

    //查询总记录数
    @Select("<script>" +
            "select count(*) from subitem where status =1" +
            "<if test='queryString!=null'> and sname like concat( '%',#{queryString},'%') </if>" +
            "</script>")
    Long count(@Param("queryString") String queryString);

    @Insert("insert into subitem (suid,sname,starttime,endtime,updatetime,pid)" +
            "values (#{suid},#{sname},#{starttime},#{endtime},#{updatetime},#{pid})")
    int addSubitem(Subitem subitem);

    //根据sid查询
    @ResultMap("SubitemMap")
    @Select("select * from subitem where status =1 and suid=#{suid}")
    Subitem findBySuid(String suid);

    //修改
    @Update("update subitem set sname=#{sname},starttime=#{starttime},pid=#{pid},endtime=#{endtime},updatetime=#{updatetime} where suid=#{suid} and status =1")
    int updateSubitem(Subitem subitem);

    //逻辑删除
    @Update("update subitem set status=0 where suid=#{suid}")
    int deleteSubitem(String suid);
}
